package com.zoffy.oj_backend.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoffy.oj_backend.common.ErrorCode;

import com.zoffy.oj_backend.exception.BusinessException;
import com.zoffy.oj_backend.judge.JudgeService;
import com.zoffy.oj_backend.model.dto.questionSubmit.QuestionSubmitAddRequest;
import com.zoffy.oj_backend.model.dto.questionSubmit.QuestionSubmitQueryRequest;
import com.zoffy.oj_backend.model.dto.questionSubmit.QuestionSubmitTinyQueryRequest;
import com.zoffy.oj_backend.model.entity.Question;
import com.zoffy.oj_backend.model.entity.QuestionSubmit;
import com.zoffy.oj_backend.model.vo.QuestionResultVo;
import com.zoffy.oj_backend.model.vo.QuestionSubmitTinyVo;
import com.zoffy.oj_backend.model.vo.QuestionSubmitVo;
import com.zoffy.oj_backend.model.vo.UserVo;
import com.zoffy.oj_backend.service.QuestionService;
import com.zoffy.oj_backend.service.QuestionSubmitService;
import com.zoffy.oj_backend.mapper.QuestionSubmitMapper;
import com.zoffy.oj_backend.service.UserService;
import com.zoffy.oj_backend.utils.SSEUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * @author 86170
 * @description 针对表【question_submit】的数据库操作Service实现
 * @createDate 2024-02-19 00:29:02
 */
@Service
public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper, QuestionSubmit>
        implements QuestionSubmitService {
    @Resource
    @Lazy
    private JudgeService judgeService;
    @Resource
    private UserService userService;
    @Resource
    private QuestionService questionService;

    @Override
    public IPage<QuestionSubmitTinyVo> listMyQuestionSubmitTinyVoByPage(HttpServletRequest request, QuestionSubmitTinyQueryRequest queryRequest) {
        int pageSize = queryRequest.getPageSize();
        int page = queryRequest.getPage();
        Long id = queryRequest.getId();
        UserVo userVo = userService.currentUserVo(request);
        QueryWrapper<QuestionSubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userVo.getId());
        queryWrapper.eq("question_id", id);
        return this.page(new Page<>(page, pageSize), queryWrapper).convert(QuestionSubmitTinyVo::obj2Vo);
    }

    @Override
    public IPage<QuestionSubmitVo> listQuestionSubmitByPage(QuestionSubmitQueryRequest questionSubmitQueryRequest) {
        int pageSize = questionSubmitQueryRequest.getPageSize();
        int page = questionSubmitQueryRequest.getPage();
        QueryWrapper<QuestionSubmit> queryWrapper = getQueryWrapper(questionSubmitQueryRequest);
        return this.page(new Page<>(page, pageSize), queryWrapper).convert(QuestionSubmitVo::obj2Vo);
    }

    @Override
    public Long doQuestionSubmit(QuestionSubmitAddRequest questionSubmitAddRequest, HttpServletRequest request) {
        String lang = questionSubmitAddRequest.getLang();
        String code = questionSubmitAddRequest.getCode();
        Long questionId = questionSubmitAddRequest.getQuestionId();
        //获取登陆用户
        UserVo userVo = userService.currentUserVo(request);
        Long userId = userVo.getId();
        //判断题目是否存在
        Question question = questionService.getById(questionId);
        if (question == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目不存在");
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        questionSubmit.setLang(lang);
        questionSubmit.setCode(code);
        questionSubmit.setJudgeInfo("{}");
        questionSubmit.setState(0);
        questionSubmit.setQuestionId(questionId);
        questionSubmit.setUserId(userId);

        this.save(questionSubmit);
        // todo 线程池改造
        CompletableFuture.runAsync(() -> {
            QuestionResultVo resultVo = judgeService.doJudge(questionSubmit.getId());

            SSEUtils.push(String.valueOf(userId), JSONUtil.toJsonStr(resultVo));
        });
        return questionSubmit.getId();
    }

    QueryWrapper<QuestionSubmit> getQueryWrapper(QuestionSubmitQueryRequest questionSubmitQueryRequest) {

        Long id = questionSubmitQueryRequest.getId();
        String lang = questionSubmitQueryRequest.getLang();
        Integer state = questionSubmitQueryRequest.getState();
        Long questionId = questionSubmitQueryRequest.getQuestionId();
        Long userId = questionSubmitQueryRequest.getUserId();


        QueryWrapper<QuestionSubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(StringUtils.isNotBlank(lang), "lang", lang);
        queryWrapper.eq(ObjectUtils.isNotEmpty(state), "state", state);
        queryWrapper.eq(ObjectUtils.isNotEmpty(questionId), "question_id", questionId);

        return queryWrapper;
    }
}




